// DlgEditRequest.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgEditRequest.h"
#include "DlgEditNotes.h"
#include "DlgConfirm.h"
#include "DlgEditLink.h"


// CDlgEditRequest dialog

IMPLEMENT_DYNAMIC(CDlgEditRequest, CDialog)

CDlgEditRequest::CDlgEditRequest(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEditRequest::IDD, pParent)
	, m_szID(_T(""))
	, m_szUID(_T(""))
	, m_szTitle(_T(""))
	, m_szDateIn(_T(""))
	, m_szDateOut(_T(""))
	, m_szViewed(_T(""))
	, m_szRedirected(_T(""))
	, m_szInstructions(_T(""))
	, m_szRequest(_T(""))
	, m_bAtLibrary(FALSE)
	, m_szUserName(_T(""))
	, m_szUserAccount(_T(""))
	, m_dServiceFee(0)
	, m_dCopyrightFee(0)
	, m_szProject(_T(""))
	, m_bInvoiced(FALSE)
	, m_dMiscFee(0)
	, m_szSource(_T(""))
	, m_szAltLocation(_T(""))
	, m_bRetain(FALSE)
{
	m_nStaffID = -1;
	m_nItemID = -1;
	m_bComplete = FALSE;
	m_bUpdateLocaiton = FALSE;
}

CDlgEditRequest::~CDlgEditRequest()
{
}

void CDlgEditRequest::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_ID, m_szID);
	DDX_Control(pDX, IDC_COMBO_STATUS, m_ctrlStatus);
	DDX_Control(pDX, IDC_COMBO_LIBRARY, m_ctrlLibrary);
	DDX_Text(pDX, IDC_EDIT_UID, m_szUID);
	DDX_Text(pDX, IDC_EDIT_SOURCE_TITLE, m_szTitle);
	DDX_Text(pDX, IDC_EDIT_DATE_IN, m_szDateIn);
	DDX_Text(pDX, IDC_EDIT_DATE_OUT, m_szDateOut);
	DDX_Text(pDX, IDC_EDIT_VIEWED, m_szViewed);
	DDX_Text(pDX, IDC_EDIT_REDIRECTED, m_szRedirected);
	DDX_Text(pDX, IDC_EDIT_INSTRUCTIONS, m_szInstructions);
	DDX_Text(pDX, IDC_EDIT_REQUEST, m_szRequest);
	DDX_Check(pDX, IDC_CHECK_AT_LIBRARY, m_bAtLibrary);
	DDX_Control(pDX, IDC_COMBO_PRIORITY, m_ctrlPriority);
	DDX_Control(pDX, IDC_COMBO_METHOD, m_ctrlMethod);
	DDX_Control(pDX, IDC_COMBO_ILL, m_ctrlILL);
	DDX_Text(pDX, IDC_EDIT_USER_NAME, m_szUserName);
	DDX_Text(pDX, IDC_EDIT_ACCOUNT_NUMBER, m_szUserAccount);
	DDX_Text(pDX, IDC_EDIT_FEE_SERVICE, m_dServiceFee);
	DDV_MinMaxDouble(pDX, m_dServiceFee, 0.00, 99999.99);
	DDX_Text(pDX, IDC_EDIT_FEE_COPYRIGHT, m_dCopyrightFee);
	DDV_MinMaxDouble(pDX, m_dCopyrightFee, 0.00, 99999.99);
	DDX_Control(pDX, IDC_COMBO_REFERRALS, m_ctrlReferrals);
	DDX_Text(pDX, IDC_EDIT_PROJECT, m_szProject);
	DDX_Check(pDX, IDC_CHECK_INVOICED, m_bInvoiced);
	DDX_Control(pDX, IDC_COMBO_COLLECTION, m_ctrlCollection);
	DDX_Text(pDX, IDC_EDIT_FEE_MISC, m_dMiscFee);
	DDX_Control(pDX, IDC_COMBO_COPYRIGHT, m_ctrlCopyright);
	DDX_Text(pDX, IDC_EDIT_SOURCE, m_szSource);
	DDX_Text(pDX, IDC_EDIT_ALT_LOCATION, m_szAltLocation);
	DDX_Check(pDX, IDC_CHECK_RETAIN, m_bRetain);
}


BEGIN_MESSAGE_MAP(CDlgEditRequest, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, &CDlgEditRequest::OnBnClickedButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_NOTES, &CDlgEditRequest::OnBnClickedButtonNotes)
	ON_CBN_SELCHANGE(IDC_COMBO_PRIORITY, &CDlgEditRequest::OnCbnSelchangeComboPriority)
	ON_CBN_SELCHANGE(IDC_COMBO_METHOD, &CDlgEditRequest::OnCbnSelchangeComboMethod)
	ON_BN_CLICKED(IDC_BUTTON_LINK, &CDlgEditRequest::OnBnClickedButtonLink)
END_MESSAGE_MAP()


// CDlgEditRequest message handlers

BOOL CDlgEditRequest::OnInitDialog()
{
	CDialog::OnInitDialog();

	// check for valid ID
	if( m_nItemID < 0 )
	{
		EndDialog( IDCANCEL );
		return TRUE;
	}

	// get string arrays if needed
	if( m_szaStatus.IsEmpty() )
		appConfig.m_SQL.GetDescriptions( CMySQLEx::status, &m_szaStatus );
	if( m_szaPriority.IsEmpty() )
		appConfig.m_SQL.GetDescriptions( CMySQLEx::priority, &m_szaPriority );
	if( m_szaMethod.IsEmpty() )
		appConfig.m_SQL.GetDescriptions( CMySQLEx::method, &m_szaMethod );
	if( m_szaLibrary.IsEmpty() )
		appConfig.m_SQL.GetDescriptions( CMySQLEx::library, &m_szaLibrary );
	if( m_szaCollection.IsEmpty() )
		appConfig.m_SQL.GetDescriptions( CMySQLEx::collection, &m_szaCollection );
	if( m_szaReferrals.IsEmpty() )
		appConfig.m_SQL.GetDescriptions( CMySQLEx::referrals, &m_szaReferrals );
	if( m_szaCopyright.IsEmpty() )
		appConfig.m_SQL.GetDescriptions( CMySQLEx::copyright, &m_szaCopyright );
	if( m_szaILL.IsEmpty() )
		appConfig.m_SQL.GetDescriptions( CMySQLEx::ill, &m_szaILL );

	// get request
	REQUEST_SET Request;
	if( !appConfig.m_SQL.GetRequestSet( &Request, m_nItemID ))
	{
		AfxMessageBox( _T("ERROR getting request.") );
		EndDialog( IDCANCEL );
		return TRUE;
	}

	INT nCurSel = 0;
	INT nSel = 0;
	CString szTemp;

	// set m_ctrlStatus
	for( INT nIndex = 0; nIndex < m_szaStatus.GetCount(); nIndex++ )
	{
		szTemp = m_szaStatus.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlStatus.AddString( szTemp );
			m_ctrlStatus.SetItemData( nSel, nIndex );
			if( Request.m_status_id == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlStatus.SetCurSel( nCurSel );

	// set m_ctrlLibrary
	for( INT nIndex = 0; nIndex < m_szaLibrary.GetCount(); nIndex++ )
	{
		szTemp = m_szaLibrary.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlLibrary.AddString( szTemp );
			m_ctrlLibrary.SetItemData( nSel, nIndex );
			if( Request.m_library_id == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlLibrary.SetCurSel( nCurSel );

	// set m_ctrlCollection
	for( INT nIndex = 0; nIndex < m_szaCollection.GetCount(); nIndex++ )
	{
		szTemp = m_szaCollection.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlCollection.AddString( szTemp );
			m_ctrlCollection.SetItemData( nSel, nIndex );
			if( Request.m_collection_id == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlCollection.SetCurSel( nCurSel );

	//m_ctrlPriority
	for( INT nIndex = 0; nIndex < m_szaPriority.GetCount(); nIndex++ )
	{
		szTemp = m_szaPriority.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlPriority.AddString( szTemp );
			m_ctrlPriority.SetItemData( nSel, nIndex );
			if( Request.m_priority_id == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlPriority.SetCurSel( nCurSel );

	//m_ctrlMethod
	for( INT nIndex = 0; nIndex < m_szaMethod.GetCount(); nIndex++ )
	{
		szTemp = m_szaMethod.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlMethod.AddString( szTemp );
			m_ctrlMethod.SetItemData( nSel, nIndex );
			if( Request.m_method_id == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlMethod.SetCurSel( nCurSel );

	//m_ctrlReferrals
	for( INT nIndex = 0; nIndex < m_szaReferrals.GetCount(); nIndex++ )
	{
		szTemp = m_szaReferrals.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlReferrals.AddString( szTemp );
			m_ctrlReferrals.SetItemData( nSel, nIndex );
			if( Request.m_referrals == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlReferrals.SetCurSel( nCurSel );

	//m_ctrlCopyright
	for( INT nIndex = 0; nIndex < m_szaCopyright.GetCount(); nIndex++ )
	{
		szTemp = m_szaCopyright.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlCopyright.AddString( szTemp );
			m_ctrlCopyright.SetItemData( nSel, nIndex );
			if( Request.m_copyright == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlCopyright.SetCurSel( nCurSel );

	//m_ctrlILL
	for( INT nIndex = 0; nIndex < m_szaILL.GetCount(); nIndex++ )
	{
		szTemp = m_szaILL.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlILL.AddString( szTemp );
			m_ctrlILL.SetItemData( nSel, nIndex );
			if( Request.m_ill_id == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlILL.SetCurSel( nCurSel );

	// fill data
	m_szID.Format( _T("%.8d"), Request.m_id );
	m_szUID = Request.m_uid;
	m_szTitle = Request.m_title;
	m_szDateIn = Request.m_date_in.Format( appConfig.m_General.szDateFormat );
	if( Request.m_date_out.GetStatus() == COleDateTime::valid )
		m_szDateOut = Request.m_date_out.Format( appConfig.m_General.szDateFormat );
	else
		m_szDateOut = _T("<not sent>");
	m_szViewed.Format( _T("%d"), Request.m_viewed );
	m_szRedirected.Format( _T("%d"), Request.m_redirected );
	m_szInstructions = Request.m_instructions;
	m_szRequest = Request.m_request;
	m_bAtLibrary = (BOOL)Request.m_atlibrary;
	m_dServiceFee = Request.m_fee_service;
	m_dMiscFee = Request.m_fee_misc;
	m_dCopyrightFee = Request.m_fee_copyright;
	m_szProject = Request.m_project;
	m_bInvoiced = Request.m_invoiced;
	m_szSource = Request.m_source;
	m_szAltLocation = Request.m_location;
	m_szLink = Request.m_link;
	m_bRetain = Request.m_retain;

	// get user
	USER_SET User;
	if( !appConfig.m_SQL.GetUserSet( &User, Request.m_user_id ))
	{
		AfxMessageBox( _T("ERROR getting user.") );
		EndDialog( IDCANCEL );
		return TRUE;
	}
	// fill data
	m_szUserName = User.m_email;
	m_szUserAccount = User.m_account_number;

	// check if editing complete
	if( m_bComplete )
	{
		GetDlgItem( IDC_COMBO_STATUS )->EnableWindow( FALSE );
		if( appConfig.m_nStaffLevel < STAFF_EDITOR )
		{
			GetDlgItem( IDC_COMBO_LIBRARY )->EnableWindow( FALSE );
			GetDlgItem( IDC_COMBO_COLLECTION )->EnableWindow( FALSE );
			GetDlgItem( IDC_COMBO_PRIORITY )->EnableWindow( FALSE );
			GetDlgItem( IDC_COMBO_METHOD )->EnableWindow( FALSE );
		}
		GetDlgItem( IDC_EDIT_UID )->EnableWindow( FALSE );
		GetDlgItem( IDC_EDIT_SOURCE_TITLE )->EnableWindow( FALSE );
		GetDlgItem( IDC_EDIT_DATE_IN )->EnableWindow( FALSE );
		GetDlgItem( IDC_EDIT_DATE_OUT )->EnableWindow( FALSE );
		GetDlgItem( IDC_EDIT_REDIRECTED )->EnableWindow( FALSE );
		GetDlgItem( IDC_EDIT_INSTRUCTIONS )->EnableWindow( FALSE );
		GetDlgItem( IDC_EDIT_REQUEST )->EnableWindow( FALSE );
		GetDlgItem( IDC_CHECK_AT_LIBRARY )->EnableWindow( FALSE );

		GetDlgItem( IDC_COMBO_REFERRALS )->EnableWindow( FALSE );
		GetDlgItem( IDC_COMBO_COPYRIGHT )->EnableWindow( FALSE );

		GetDlgItem( IDC_COMBO_ILL )->EnableWindow( FALSE );
		GetDlgItem( IDC_CHECK_RETAIN )->EnableWindow( FALSE );
	}
	else
	{
		GetDlgItem( IDC_CHECK_INVOICED )->EnableWindow( FALSE );
	}

	// set location from user if request location is empty
	if( m_szAltLocation.IsEmpty() )
	{
		if( appConfig.m_Options.bUseComapnyForLocation )
			m_szAltLocation = User.m_company;
		else
			m_szAltLocation = User.m_address1;
		// disable editing locaiton
		GetDlgItem( IDC_EDIT_ALT_LOCATION )->EnableWindow( FALSE );
	}
	else
	{
		// update location
		m_bUpdateLocaiton = TRUE;
	}

	// check for link
	if( Request.m_link.IsEmpty() )
	{
		GetDlgItem( IDC_BUTTON_LINK )->EnableWindow( FALSE );
	}

	// disable ILL
	if( appConfig.m_Options.bDisableILL )
	{
		GetDlgItem( IDC_STATIC_ILL )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_COMBO_ILL )->ShowWindow( SW_HIDE );
	}

	// show retain
	if( appConfig.m_General.nHoursClaimLimit == 0 )
	{
		GetDlgItem( IDC_CHECK_RETAIN )->ShowWindow( SW_HIDE );
	}
	else
	{
		if( Request.m_status_id == STATUS_ID_PENDING )
			GetDlgItem( IDC_CHECK_RETAIN )->EnableWindow( TRUE );
		else
			GetDlgItem( IDC_CHECK_RETAIN )->EnableWindow( FALSE );
	}

	// show finalcials
	if( appConfig.m_Options.bDisableFinacial )
	{		
		GetDlgItem( IDC_CHECK_INVOICED )->ShowWindow( SW_HIDE );

		GetDlgItem( IDC_STATIC_FEE_SERVICE )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_FEE_SERVICE )->ShowWindow( SW_HIDE );

		GetDlgItem( IDC_STATIC_FEE_MISC )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_FEE_MISC )->ShowWindow( SW_HIDE );

		GetDlgItem( IDC_STATIC_FEE_COPYRIGHT )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_FEE_COPYRIGHT )->ShowWindow( SW_HIDE );
	}

	// Disable Referrals
	if( appConfig.m_Options.bDisableReferrals )
	{
		GetDlgItem( IDC_STATIC_REFERRALS )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_COMBO_REFERRALS )->ShowWindow( SW_HIDE );		
	}
	// Disable Copyright
	if( appConfig.m_Options.bDisableCopyright )
	{
		GetDlgItem( IDC_STATIC_COPYRIGHT )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_COMBO_COPYRIGHT )->ShowWindow( SW_HIDE );
	}
	// disable at library
	if( appConfig.m_Options.bDisableAtLibrary )
		GetDlgItem( IDC_CHECK_AT_LIBRARY )->ShowWindow( SW_HIDE );

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgEditRequest::OnOK()
{
	// update data
	UpdateData();

	REQUEST_SET Request;
	// get request
	appConfig.m_SQL.GetRequestSet( &Request, m_nItemID );

	// get status id
	Request.m_status_id = m_ctrlStatus.GetItemData( m_ctrlStatus.GetCurSel() );
	if( Request.m_status_id < 0 )
		return;

	// get library id
	Request.m_library_id = m_ctrlLibrary.GetItemData( m_ctrlLibrary.GetCurSel() );
	if( Request.m_library_id < 0 )
		return;

	// get collection id
	Request.m_collection_id = m_ctrlCollection.GetItemData( m_ctrlCollection.GetCurSel() );
	if( Request.m_collection_id < 0 )
		return;

	// get priority id
	Request.m_priority_id = m_ctrlPriority.GetItemData( m_ctrlPriority.GetCurSel() );
	if( Request.m_priority_id < 0 )
		return;

	// get method id
	Request.m_method_id = m_ctrlMethod.GetItemData( m_ctrlMethod.GetCurSel() );
	if( Request.m_method_id < 0 )
		return;

	// get referrals
	Request.m_referrals = m_ctrlReferrals.GetItemData( m_ctrlReferrals.GetCurSel() );
	if( Request.m_referrals < 0 )
		return;

	// get copyright
	Request.m_copyright = m_ctrlCopyright.GetItemData( m_ctrlCopyright.GetCurSel() );
	if( Request.m_copyright < 0 )
		return;

	// get ill id
	Request.m_ill_id = m_ctrlILL.GetItemData( m_ctrlILL.GetCurSel() );
	if( Request.m_ill_id < 0 )
		return;

	// fill data
	Request.m_uid = m_szUID;
	Request.m_title = m_szTitle;
	Request.m_source = m_szSource;
	Request.m_viewed = _tstol( m_szViewed );
	Request.m_redirected = _tstol( m_szRedirected );
	Request.m_instructions = m_szInstructions;
	Request.m_request = m_szRequest;
	Request.m_atlibrary = (INT)m_bAtLibrary;
	Request.m_fee_copyright = m_dCopyrightFee;
	Request.m_fee_service = m_dServiceFee;
	Request.m_fee_misc = m_dMiscFee;
	Request.m_project = m_szProject;
	Request.m_invoiced = m_bInvoiced;
	Request.m_retain = m_bRetain;

	// set location if editable
	if( m_bUpdateLocaiton )
		Request.m_location = m_szAltLocation;

	// save link
	Request.m_link = m_szLink;

	// set touched
	Request.m_touched = TRUE;

	// update request
	appConfig.m_SQL.SetRequestSet( &Request );

	// write history
	appConfig.UpdateRequestHistory( m_nItemID, m_nStaffID, Request.m_status_id, _T("Edited") );

	// close dialog
	CDialog::OnOK();
}

void CDlgEditRequest::OnBnClickedButtonDelete()
{
	// confirm
	CDlgConfirm dlgConfirm;
	dlgConfirm.m_szAction = _T("WARNING:\n\nThis will permanatly delete this request.\n\nAre you sure you want to continue?");
	if( dlgConfirm.DoModal() != IDOK )
		return;

	REQUEST_SET Request;
	// get request
	appConfig.m_SQL.GetRequestSet( &Request, m_nItemID );

	// write history
	appConfig.UpdateRequestHistory( m_nItemID, m_nStaffID, Request.m_status_id, _T("Deleted") );

	// remove request
	appConfig.m_SQL.RemoveRequest( m_nItemID );

	// close dialog
	CDialog::OnOK();
}

void CDlgEditRequest::OnBnClickedButtonNotes()
{
	// edit notes
	CDlgEditNotes dlgNotes;
	dlgNotes.m_nItemID = m_nItemID;
	dlgNotes.DoModal();
}

void CDlgEditRequest::OnCbnSelchangeComboPriority()
{
	LONG nID;
	CString szTemp;
	DOUBLE dFeeTotal = 0;

	// update data
	UpdateData();

	// get priority id
	nID = m_ctrlPriority.GetItemData( m_ctrlPriority.GetCurSel() );
	if( nID >= 0 )
	{
		// get method fee
		PRIORITY_LOOKUP Priority;
		appConfig.m_SQL.GetDescriptionPriority( &Priority, nID );
		dFeeTotal += Priority.dServiceFee;
	}

	// get method id
	nID = m_ctrlMethod.GetItemData( m_ctrlMethod.GetCurSel() );
	if( nID >= 0 )
	{
		// get method fee
		METHOD_LOOKUP Method;
		appConfig.m_SQL.GetDescriptionMethod( &Method, nID );
		dFeeTotal += Method.dServiceFee;
	}

	// set new total
	m_dServiceFee = dFeeTotal;
	// update data
	UpdateData( FALSE );
}

void CDlgEditRequest::OnCbnSelchangeComboMethod()
{
	LONG nID;
	CString szTemp;
	DOUBLE dFeeTotal = 0;

	// update data
	UpdateData();

	// get priority id
	nID = m_ctrlPriority.GetItemData( m_ctrlPriority.GetCurSel() );
	if( nID >= 0 )
	{
		// get method fee
		PRIORITY_LOOKUP Priority;
		appConfig.m_SQL.GetDescriptionPriority( &Priority, nID );
		dFeeTotal += Priority.dServiceFee;
	}

	// get method id
	nID = m_ctrlMethod.GetItemData( m_ctrlMethod.GetCurSel() );
	if( nID >= 0 )
	{
		// get method fee
		METHOD_LOOKUP Method;
		appConfig.m_SQL.GetDescriptionMethod( &Method, nID );
		dFeeTotal += Method.dServiceFee;
	}

	// set new total
	m_dServiceFee = dFeeTotal;
	// update data
	UpdateData( FALSE );
}


void CDlgEditRequest::OnBnClickedButtonLink()
{
	CDlgEditLink dlgEditLink;
	dlgEditLink.m_szURL = m_szLink;
	// show dialog
	if( dlgEditLink.DoModal() == IDOK )
		m_szLink = dlgEditLink.m_szURL;
}
